$(function(){
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    new FastClick(document.body);
    //注册以及隐私条款的阅读
    var flag = true;
    $(".lable").on("click",function(){
        flag = !flag;
        if(flag) {
            $(".lable img").attr("src","../../static/img/ico_select@3x.png");
        }else {
            $(".lable img").attr("src","../../static/img/ico_selectk@3x.png");
        }
    });
    //获取验证码
    var flagL = false;
    $(".counter").on("click",function() {
        if(flagL) return false;
        var mobile = $(".phone").val();
        if(!mobile) {
            tips("请输入手机号码");
            return false;
        }
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){ 
            tips("请输入正确的手机号码");
            return false; 
        }
        $.ajax({
            type: "post",
            url: domain.test + "/gettelcode",
            data: {
                mobile:mobile
            },
            beforeSend: function (){
                $(".loadingMarsk").css("display","block");
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                $(".loadingMarsk").css("display","none");                
                    var counter = 90;
                    var timer = setInterval(function() {
                        counter--;
                        if(counter===0) {
                            $(".counter").html("获取短息验证码");
                            clearInterval(timer);
                        }else {
                            $(".counter").html(counter + "s");
                            flagL = true;
                        }
                    },1000);
                }
            }
        });
    });
    //注册
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".regBtn").on("click",function(){
        var userName = $(".userName").val();
        if(!userName) {
            tips("请输入注册账号");
            return false;
        }
        if(userName.length<6||userName.length>20) {
            tips("注册账号长度必须位6-20位");
            return false;
        }
        var password = $(".password").val();
        if(!password) {
            tips("请输入密码");
            return false;
        }
        if(password.length<6||password.length>20) {
            tips("密码长度必须位6-20位");
        }
        var mobile = $(".phone").val();
        if(!mobile) {
            tips("请输入手机号码");
            return false;
        }
        if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(mobile))){ 
            tips("请输入正确的手机号码");
            return false; 
        }
        var code = parseInt($(".code").val());
        if(!code) {
            tips("请输入验证码");
            return false;
        }
        console.log(flag);
        if(!flag) {
            tips("请确认协议和隐私政策");
            return false;
        };
        $.ajax({
            type: "post",
            url: domain.test + "/reg",
            data: {
                userName:userName,
                password:password,
                mobile:mobile,
                captcha:1234,
                os:"iOS",
                pushId:"sir555"
            },
            beforeSend: function () {
                $(".loadingMarsk").css("display","block");                
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                    $(".loadingMarsk").css("display","none");                
                    var userData = response.data.data;
                    localStorage.token = userData.token;
                    localStorage.userName = userData.userName;
                    localStorage.userMobile = userData.mobile
                    location.href = "../me/me.html";
                }
                if(response.data.rtnCode===400) {
                    $(".loadingMarsk").css("display","none");                
                    tips(response.data.msg);
                }
            }
        });
    });
})